const StartYear = 1979
const EndYear = 2049

const yearOptions = () => {
  const yearLength = EndYear - StartYear

  const list: { label: string; value: string }[] = []
  for (let i = 0; i < yearLength; i++) {
    list.push({
      label: `${StartYear + i}年`,
      value: `${StartYear + i}`,
    })
  }

  return list
}

const monthOptions = () => {
  const monthLength = 12 - 0

  const list: { label: string; value: string }[] = []
  for (let i = 0; i < monthLength; i++) {
    list.push({
      label: `${1 + i}月`,
      value: `${1 + i}`,
    })
  }
  return list
}
import { onLoad } from '@dcloudio/uni-app'
import dayjs from 'dayjs'

export function useDate() {
  let yearList: { label: string; value: string }[] = []
  let monthList: { label: string; value: string }[] = []

  const getPickerNowIndex = () => {
    const yearIndex = yearList.findIndex((c) => `${c.value}` === `${dayjs().year()}`)
    const monthIndex = monthList.findIndex((c) => `${c.value}` === `${dayjs().month() + 1}`)

    return [yearIndex, monthIndex]
  }

  onLoad(() => {
    yearList = yearOptions()
    monthList = monthOptions()
  })

  return { yearList, monthList, getPickerNowIndex }
}
